classdef SinkToFileDetail
    properties
        repeat = 0;
        time = [];
        value = [];
        VariableName = '';
        VariableUnit = '';
    end
    
    properties(Dependent = true)
        mean_value = 0;
        std_value = 0;
    end
    
    methods
        function obj = SinkToFileDetail(varargin)
            % Supported syntax:
            % rd = SinkToFileDetail(filename);
            % rd = SinkToFileDetail(FilenamePattern,ID_Array)
            % rd = SinkToFileDetail(FilenamePattern,ID_Array,time_to_report)
            if nargin==1
                % rd = SinkToFileDetail(filename);
                filename = varargin{1};
                fid = fopen(filename);
                if fid < 3
                    error('Invalid file: %s\n',filename);
                end
                data = textscan(fid,'%f %f');
                time_temp = data{1};
                value_temp = data{2};
                for i = 1:length(time_temp)
                    if(time_temp(i+1)<time_temp(i))
                        break;
                    end
                end
                num_records_per_run = i;
                num_repeats = length(time_temp)/num_records_per_run;
                obj.time = time_temp(1:num_records_per_run);
                obj.value = zeros(num_records_per_run,num_repeats);
                for j = 1:num_repeats
                    obj.value(:,j) = value_temp((j-1)*num_records_per_run+1:j*num_records_per_run);
                end
                obj.repeat = num_repeats;
            elseif nargin == 2
                
            else
                
            end
        end
        
        function record3 = merge(record1,record2)
            % merge the simulation results of multiple runs
            record3 = SinkToFileDetail();
            assert(all(record1.time==record2.time),'Cannot merge to records for different time');
            record3.time = record1.time;
            record3.value = [record1.value,record2.value];
            record3.repeat = record1.repeat+record2.repeat;
        end
        
        function [h,mean_value,std_value] = hist(obj,time,varargin)
            data = obj.value(obj.time==time,:);
            if nargin == 4
                h = myhist(data,varargin{1},varargin{2});
            else
                median_data = median(data);
                HalfRegionWidth = max(max(data)-median_data,median_data-min(data));
                h = myhist(data,[median_data-HalfRegionWidth,median_data+HalfRegionWidth],30);
            end
            set(gca,'FontSize',15);
=======
            if nargin == 4
                h = myhist(data,varargin{1},varargin{2});
            else
                median_data = median(data);
                HalfRegionWidth = max(max(data)-median_data,median_data-min(data));
                h = myhist(data,[median_data-HalfRegionWidth,median_data+HalfRegionWidth],30);
            end
            set(gca,'FontSize',20);
>>>>>>> .r110
            if isempty(obj.VariableUnit)
                xlabel(sprintf('%s',obj.VariableName));
            else
                xlabel(sprintf('%s (%s)',obj.VariableName,obj.VariableUnit));
            end
            ylabel('Count');
            y_limit = get(gca,'Ylim');
            ylim([y_limit(1),1.25*y_limit(2)]);
            x_limit = get(gca,'Xlim');
            y_limit = get(gca,'Ylim');
%             title(sprintf('mean = %.4estd = %.4e',mean(data),std(data)));
            x_pos = x_limit(1)+0.55*(x_limit(2)-x_limit(1));
            y_pos = y_limit(1)+0.90*(y_limit(2)-y_limit(1));
            text(x_pos,y_pos,sprintf('mean = %.4e\n   std = %.4e',mean(data),std(data)),'FontSize',15);
            mean_value = mean(data);
            std_value = std(data);
        end
    end
    
    methods        
        function m = get.mean_value(obj)
            m = mean(obj.value,2);
        end
        
        function s = get.std_value(obj)
            s = std(obj.value,1,2);
        end
        
        function m = cal_mean(obj)
            m = mean(obj.value,2);
        end
        
        function s = cal_std(obj)
            s = std(obj.value,1,2);
        end
        
        function STF = ConvertToSinkToFile(obj)
            STF = SinkToFile();
            STF.time = obj.time;
            STF.mean = obj.cal_mean();
            STF.var = (obj.cal_std()).^2;
            STF.VariableName = obj.VariableName;
            STF.VariableUnit = obj.VariableUnit;
        end
    end
end